from datetime import datetime

from sqlalchemy import Column, Integer, String, Boolean, Float, ForeignKey, UniqueConstraint, Index, orm, DECIMAL
from sqlalchemy.dialects.mysql import LONGTEXT

from app.libs.tools import number2string
from app.models.base import Base, db


class order_info(Base):
    id = Column(Integer, autoincrement=True, primary_key=True)
    order_number = Column(String(30), nullable=False, default='')
    title = Column(String(100), nullable=False, default='')
    author = Column(String(50), nullable=True)
    chars_sum = Column(Integer, default=0)
    similar_rate = Column(DECIMAL(5, 2), default=0)
    completed = Column(Integer, default=0)
    taobao_orders = Column(String(30), default='0')
    submit_title = Column(String(150), unique=True, default='')
    is_delete = Column(Integer, default=0)
    file_path = Column(String(250), default='')
    user = Column(Integer, default=0)
    pay_type = Column(String(20), default='taobao')
    ai_lock = Column(Integer, default=1)
    out_trade_no = Column(String(50), default='')

    def keys(self):
        return ['order_number', 'title', 'author', 'rate', 'chars_sum', 'finished', 'create_datetime', 'is_delete',
                'user']

    @property
    def rate(self):
        return number2string(self.similar_rate * 100) + '%'

    @property
    def finished(self):
        if int(datetime.now().timestamp()) - self.create_time > 86400 * 7:
            return '报告已过期删除'
        return '正在比对' if self.completed != 1 else '已完成'
